package com.lqq.eduservice.controller.fron;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqq.commonutils.R;
import com.lqq.eduservice.entity.EduCourse;
import com.lqq.eduservice.entity.EduTeacher;
import com.lqq.eduservice.service.EduCourseService;
import com.lqq.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/teacherfront")
//@CrossOrigin
public class TeacherFrontController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    //分页查询讲师的方法
    @GetMapping("/getTeacherFrontList/{current}/{limit}")
    public R getTeacherFrontList(@PathVariable("current") long current,
                                 @PathVariable("limit") long limit) {
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        Map<String, Object> map = teacherService.getTeacherFrontList(pageTeacher);
        //返回分页中所有数据
        return R.ok().data(map);
    }

    //讲师详情
    @GetMapping("/getTeacherFrontInfo/{teacherId}")
    public R getTeacherFrontInfo(@PathVariable("teacherId") String teacherId) {
        //根据讲师id查询讲师基本信息
        EduTeacher teacher = teacherService.getById(teacherId);

        //根据讲师id查询讲师所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", teacherId);
        List<EduCourse> courses = courseService.list(wrapper);

        return R.ok().data("teacher", teacher).data("courseList", courses);
    }
}
